<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>发布动态 - 测盟汇系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.ckeditor.com/ckeditor5/38.1.1/classic/ckeditor.js"></script>
    <style>
        :root {
            --primary-color: #2c5545;
            --primary-light: #3a7d62;
            --primary-hover: #1e4636;
            --dynamic-color: #e67e22;
            --text-color: #333;
            --text-light: #555;
            --border-color: #d1d9d6;
            --bg-light: #f5f7f6;
            --bg-page: #e8ecea;
            --white: #fff;
            --shadow: 0 4px 24px rgba(44, 85, 69, 0.1);
            --radius: 12px;
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: var(--bg-page);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: var(--text-color);
            line-height: 1.6;
        }

        .header {
            background: var(--primary-color);
            color: var(--white);
            padding: 1rem 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .logo {
            display: flex;
            align-items: center;
        }

        .logo i {
            font-size: 1.8rem;
            margin-right: 0.8rem;
        }

        .logo h1 {
            font-size: 1.5rem;
            font-weight: 600;
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .user-info i {
            font-size: 1.2rem;
            margin-right: 0.5rem;
        }

        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: var(--white);
            padding: 0.5rem 1rem;
            border-radius: 4px;
            cursor: pointer;
            transition: var(--transition);
            margin-left: 1rem;
            display: flex;
            align-items: center;
        }

        .logout-btn i {
            margin-right: 0.5rem;
        }

        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .container {
            max-width: 1000px;
            margin: 2rem auto;
            padding: 0 2rem;
        }

        .content-card {
            background: var(--white);
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            padding: 2rem;
            margin-bottom: 2rem;
            border: 1px solid var(--border-color);
        }

        .page-title {
            color: var(--primary-color);
            font-size: 1.8rem;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
        }

        .page-title i {
            margin-right: 1rem;
        }

        .action-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
        }

        .btn {
            padding: 0.8rem 1.5rem;
            border-radius: var(--radius);
            border: none;
            cursor: pointer;
            font-weight: 500;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
        }

        .btn i {
            margin-right: 0.5rem;
        }

        .btn-primary {
            background: var(--primary-color);
            color: var(--white);
        }

        .btn-primary:hover {
            background: var(--primary-hover);
            transform: translateY(-2px);
        }

        .btn-secondary {
            background: var(--bg-light);
            color: var(--text-color);
            border: 1px solid var(--border-color);
        }

        .btn-secondary:hover {
            background: #e1e8e4;
            transform: translateY(-2px);
        }

        .btn-dynamic {
            background: var(--dynamic-color);
            color: var(--white);
        }

        .btn-dynamic:hover {
            background: #d35400;
            transform: translateY(-2px);
        }

        .form-section {
            margin-bottom: 2rem;
            padding: 1.5rem;
            background: var(--bg-light);
            border-radius: var(--radius);
            border-left: 4px solid var(--dynamic-color);
        }

        .form-section-title {
            color: var(--primary-color);
            font-weight: 600;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
        }

        .form-section-title i {
            margin-right: 0.5rem;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            color: var(--primary-color);
            font-weight: 500;
        }

        .form-control {
            width: 100%;
            padding: 0.8rem;
            border: 1px solid var(--border-color);
            border-radius: var(--radius);
            font-size: 1rem;
            background: var(--bg-light);
            transition: var(--transition);
        }

        .form-control:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 2px rgba(44, 85, 69, 0.2);
            background: var(--white);
        }

        .form-text {
            font-size: 0.85rem;
            color: var(--text-light);
            margin-top: 0.5rem;
        }

        .cover-preview {
            max-width: 300px;
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            margin-top: 1rem;
        }

        .ck-editor__editable {
            min-height: 300px;
            border-radius: 0 0 var(--radius) var(--radius) !important;
        }

        .ck.ck-toolbar {
            border-radius: var(--radius) var(--radius) 0 0 !important;
        }

        .action-buttons {
            display: flex;
            justify-content: flex-end;
            gap: 1rem;
            margin-top: 2rem;
        }

        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                text-align: center;
                padding: 1rem;
            }

            .user-info {
                margin-top: 1rem;
            }

            .action-bar {
                flex-direction: column;
                gap: 1rem;
                align-items: flex-start;
            }
        }
    </style>
</head>
<body>
<div class="header">
    <div class="logo">
        <i class="fas fa-store"></i>
        <h1>测盟汇系统</h1>
    </div>
    <div class="user-info">
        <i class="fas fa-user-shield"></i>
        <span>超级管理员：<span th:text="${session.user.username}"></span></span>
        <a th:href="@{/user/logout}" class="logout-btn">
            <i class="fas fa-sign-out-alt"></i>
            退出登录
        </a>
    </div>
</div>

<div class="container">
    <div class="content-card">
        <div class="action-bar">
            <div>
                <a href="/admin_list" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> 返回列表
                </a>
            </div>
            <h2 class="page-title">
                <i class="fas fa-plus-circle"></i> 发布新动态
            </h2>
        </div>

        <form id="publishForm">
            <div class="form-section">
                <div class="form-section-title">
                    <i class="fas fa-info-circle"></i> 基本信息
                </div>

                <div class="form-group">
                    <label for="title">标题</label>
                    <input type="text" id="title" class="form-control" placeholder="请输入动态标题" required>
                </div>

                <div class="form-group">
                    <label for="author">作者</label>
                    <input type="text" id="author" class="form-control" placeholder="请输入作者名称" required>
                </div>

                <div class="form-group">
                    <label for="introduction">简介</label>
                    <textarea id="introduction" class="form-control" rows="3" placeholder="请输入简短简介" required></textarea>
                    <div class="form-text">简介将显示在动态列表页，吸引读者点击</div>
                </div>
            </div>

            <div class="form-section">
                <div class="form-section-title">
                    <i class="fas fa-file-alt"></i> 内容编辑
                </div>

                <div class="form-group">
                    <label for="content">内容</label>
                    <textarea id="content" name="content"></textarea>
                </div>
            </div>

            <div class="form-section">
                <div class="form-section-title">
                    <i class="fas fa-image"></i> 封面设置
                </div>

                <div class="form-group">
                    <label for="coverUrl">封面图片</label>
                    <input type="file" id="coverUrl" class="form-control" accept="image/*">
                    <div class="form-text">建议尺寸：1200×630像素，支持JPG/PNG格式，大小不超过5MB</div>
                    <div id="coverPreview" class="mt-2"></div>
                </div>
            </div>

            <div class="action-buttons">
                <button type="button" class="btn btn-dynamic" onclick="publishDynamic()">
                    <i class="fas fa-paper-plane"></i> 发布动态
                </button>
            </div>
        </form>
    </div>
</div>

<script>
    let editor;
    ClassicEditor
        .create(document.querySelector('#content'), {
            toolbar: [
                'heading', '|',
                'bold', 'italic', 'underline', 'strikethrough', '|',
                'alignment', '|',
                'numberedList', 'bulletedList', '|',
                'outdent', 'indent', '|',
                'link', 'image', 'table', '|',
                'blockQuote', 'codeBlock', '|',
                'undo', 'redo'
            ],
            image: {
                toolbar: [
                    'imageStyle:inline', 'imageStyle:block', 'imageStyle:side',
                    '|', 'imageTextAlternative'
                ],
                upload: {
                    url: '/dynamic/upload-image'
                }
            }
        })
        .then(newEditor => {
            editor = newEditor;
        })
        .catch(error => {
            console.error('CKEditor初始化失败:', error);
        });

    document.getElementById('coverUrl').addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(event) {
                document.getElementById('coverPreview').innerHTML = `
                    <img src="${event.target.result}" class="cover-preview" alt="封面预览">
                `;
            };
            reader.readAsDataURL(file);
        }
    });

    function publishDynamic() {
        if (!editor) {
            alert('编辑器尚未准备就绪，请稍后再试');
            return;
        }

        const title = ($('#title').val() || '').trim();
        const author = ($('#author').val() || '').trim();
        const introduction = ($('#introduction').val() || '').trim();
        const content = (editor.getData() || '').trim();
        const coverFile = $('#coverUrl')[0]?.files[0];

        if (!title) { alert('请输入标题'); return; }
        if (!author) { alert('请输入作者'); return; }
        if (!introduction) { alert('请输入简介'); return; }
        if (!content) { alert('请输入内容'); return; }

        const dynamicData = {
            title: title,
            author: author,
            introduction: introduction,
            content: content,
            status: 0
        };

        let dynamicJson;
        try {
            dynamicJson = JSON.stringify(dynamicData);
        } catch (e) {
            alert('数据格式错误：' + e.message);
            return;
        }

        const formData = new FormData();
        formData.append('dynamic', dynamicJson);
        if (coverFile) {
            formData.append('coverUrl', coverFile);
        }

        $.ajax({
            url: '/dynamic/publish',
            method: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            success: function(response) {
                if (response.code === 200) {
                    alert('发布成功');
                    window.location.href = '/admin_list';
                } else {
                    alert('发布失败：' + response.message);
                }
            },
            error: function(xhr) {
                alert('请求错误：' + (xhr.responseText || '服务器异常'));
            }
        });
    }
</script>
</body>
</html>